<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:p="http://primefaces.org/ui">

<ui:composition template="/template.xhtml">
	<ui:define name="title">
		SGMF :: Nota Fiscal
	</ui:define>

	<ui:define name="content">
		<p:panel>
			<h:form id="buscarFornecedorForm">
				<p:fieldset legend="Buscar Fornecedor">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Razão Social" for="razaoSocial" />
						<p:inputText id="razaoSocial" value="#{notaFiscalBean.razaoSocial}" />
						<p:commandButton id="buscarFornecedor" update=":fornecedorDialogForm" actionListener="#{notaFiscalBean.buscarFornecedor}" value="Buscar Fornecedor" icon="ui-icon-search" onclick="PF('fornecedorDialog').show();" />
					</p:panelGrid>
				</p:fieldset>
			</h:form>
			<br />
			<h:form id="buscarPecaForm">
				<p:fieldset legend="Buscar Peça">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Nome da Peça" for="nomePeca" />
						<p:inputText id="nomePeca" value="#{notaFiscalBean.nome}" />
						<p:commandButton id="buscarPeca" update=":pecaDialogForm" actionListener="#{notaFiscalBean.buscarPeca}" value="Buscar Peça" icon="ui-icon-search" onclick="PF('pecaDialog').show();" />
					</p:panelGrid>
				</p:fieldset>
			</h:form>
			<br />
			<h:form id="notaFiscalForm">
				<p:fieldset legend="Dados da Nota Fiscal">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Fornecedor" for="fornecedor" />
						<h:outputText id="fornecedor" value="#{notaFiscalBean.fornecedor.razaoSocial}" />
						<h:outputText />
						<p:outputLabel value="Número" for="numero" />
						<p:inputText id="numero" label="Número" value="#{notaFiscalBean.notaFiscal.numero}" required="true" />
						<p:message for="numero" />
						<p:outputLabel value="Série" for="serie" />
						<p:inputText id="serie" label="Série" value="#{notaFiscalBean.notaFiscal.serie}" required="true" />
						<p:message for="serie" />
						<p:outputLabel value="Data de Emissão" for="dataEmissao" />
						<p:calendar id="dataEmissao" label="Data de Emissão" value="#{notaFiscalBean.notaFiscal.dataEmissao}" required="true" />
						<p:message for="dataEmissao" />
						<p:outputLabel value="Chave de Acesso" for="chave" />
						<p:inputText id="chave" label="Chave de Acesso" value="#{notaFiscalBean.notaFiscal.chave}" required="true" />
						<p:message for="chave" />
						<p:outputLabel value="Modelo NF" for="modeloNF" />
						<p:selectOneMenu id="modeloNF" value="#{notaFiscalBean.notaFiscal.modeloNF}" required="true">
							<f:selectItems value="#{notaFiscalBean.modelosNF}" var="modeloNF" itemValue="#{modeloNF}" itemLabel="#{modeloNF.tipo}" />
						</p:selectOneMenu>
						<p:message for="modeloNF" />
						<p:outputLabel value="Dados Adicionais" for="dadosAdicionais" />
						<p:inputTextarea id="dadosAdicionais" value="#{notaFiscalBean.notaFiscal.dados}" rows="5" cols="30" maxlength="255" />
						<p:message for="dadosAdicionais" />
					</p:panelGrid>
				</p:fieldset>
				<br/>
				<p:dataTable id="notaFiscalTable" var="item"
								 value="#{notaFiscalBean.itensNota}" rows="5" paginator="true"
								 paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
								 currentPageReportTemplate="({currentPage} de {totalPages})" emptyMessage="Nenhum registro encontrado." rendered="#{notaFiscalBean.itensNota != null}">
						<p:column headerText="Nome">
							<h:outputText value="#{item.peca.nome}" />
						</p:column>
						<p:column headerText="Quantidade">
							<h:outputText id="quantidade" value="#{item.quantidade}" />
						</p:column>
						<p:column headerText="Preço Unitario">
							<h:outputText value="#{item.peca.preco}">
								<f:convertNumber type="currency" locale="pt_BR" />
							</h:outputText>
						</p:column>
						<p:column headerText="Preço Total">
							<h:outputText value="#{item.peca.preco * item.quantidade}">
								<f:convertNumber type="currency" locale="pt_BR" />
							</h:outputText>
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-carat-1-n" title="Incrementar" process="@this" update="notaFiscalTable" actionListener="#{notaFiscalBean.incrementarItemNota(item)}" />
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-carat-1-s" title="Decrementar" rendered="#{item.quantidade == 1}" disabled="true" />
							<p:commandButton icon="ui-icon-carat-1-s" title="Decrementar" process="@this" update="notaFiscalTable" actionListener="#{notaFiscalBean.decrementarItemNota(item)}" rendered="#{item.quantidade > 1}" />
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-trash" title="Remover" process="@this" update="notaFiscalTable" actionListener="#{notaFiscalBean.removerItemNota(item)}" />
						</p:column>
					</p:dataTable>
				<br/>
				<p:commandButton id="salvar" value="Salvar"	action="#{notaFiscalBean.salvar}" update=":growl, :notaFiscalForm" icon="ui-icon-disk" />
				<p:commandButton id="novo" value="Novo"	actionListener="#{notaFiscalBean.novo}" process="@this" update=":notaFiscalForm" icon="ui-icon-document" />
			</h:form>
		</p:panel>
		
		<p:dialog id="fornecedorDialog" header="Fornecedores" widgetVar="fornecedorDialog" modal="true" resizable="false" width="800px">
			<h:form id="fornecedorDialogForm">
				<p:dataTable id="fornecedorDialogTable" var="fornecedor"
					value="#{notaFiscalBean.fornecedores}" rows="5" paginator="true"
					paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
					currentPageReportTemplate="({currentPage} de {totalPages})"
					emptyMessage="Nenhum registro encontrado."
					rendered="#{notaFiscalBean.fornecedores != null}">
					<p:column headerText="CNPJ">
						<h:outputText value="#{fornecedor.cnpj}" />
					</p:column>
					<p:column headerText="Razão Social">
						<h:outputText value="#{fornecedor.razaoSocial}" />
					</p:column>
					<p:column headerText="Nome Fantasia">
						<h:outputText value="#{fornecedor.nomeFantasia}" />
					</p:column>
					<p:column style="width: 32px;">
						<p:commandButton icon="ui-icon-plus" title="Adicionar" actionListener="#{notaFiscalBean.adicionarFornecedor(fornecedor)}" update=":notaFiscalForm:notaFiscalTable" ajax="false" />
					</p:column>
				</p:dataTable>
			</h:form>
		</p:dialog>
		
		<p:dialog id="pecaDialog" header="Peças" widgetVar="pecaDialog" modal="true" resizable="false" width="800px">
			<h:form id="pecaDialogForm">
				<p:dataTable id="pecaDialogTable" var="peca"
					value="#{notaFiscalBean.pecas}" rows="5" paginator="true"
					paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
					currentPageReportTemplate="({currentPage} de {totalPages})"
					emptyMessage="Nenhum registro encontrado."
					rendered="#{notaFiscalBean.pecas != null}">
					<p:column headerText="Nome">
						<h:outputText value="#{peca.nome}" />
					</p:column>
					<p:column headerText="Preço">
						<h:outputText value="#{peca.preco}">
							<f:convertNumber type="currency" locale="pt_BR" />
						</h:outputText>
					</p:column>
					<p:column style="width: 32px;">
						<p:commandButton icon="ui-icon-plus" title="Adicionar" actionListener="#{notaFiscalBean.adicionarPeca(peca)}" update=":notaFiscalForm:notaFiscalTable" />
					</p:column>
				</p:dataTable>
			</h:form>
		</p:dialog>
	</ui:define>
</ui:composition>

</html>